package com.hegaoye.account.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.hegaoye.core.base.BaseDAO;
import com.hegaoye.core.base.BaseSVImpl;
import com.hegaoye.core.entity.Page;
import com.hegaoye.core.exceptions.AccountException;
import com.hegaoye.core.exceptions.BaseException;
import com.hegaoye.account.dao.AccountDAO;
import com.hegaoye.account.entity.Account;
import com.hegaoye.account.entity.AccountState;
import com.hegaoye.account.feignclient.AccountFeignApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("accountSV")
@Slf4j
public class AccountSVImpl extends BaseSVImpl<Account, Long> implements AccountSV {

  @Autowired private AccountDAO accountObjDAO;

  @Resource private UidGenerator uidGenerator;

  @Autowired private AccountFeignApi accountFeignApi;

  @Override
  protected BaseDAO getBaseDAO() {
    return accountObjDAO;
  }

  /**
   * 保存account对象
   *
   * @param entity 实体
   */
  @Override
  public void save(Account entity) throws BaseException {
    entity.setCode(String.valueOf(uidGenerator.getUID()));
    entity.setState(AccountState.Activate.name());
    entity.setCreateTime(new Date());
    entity.setUpdateTime(new Date());
    super.save(entity);
  }

  @Override
  public void modify(Account entity) throws BaseException {
    entity.setUpdateTime(new Date());
    super.modify(entity);
  }

  /**
   * 查询账户
   *
   * @param account 编码
   * @return 用户
   */
  @Override
  public String load(String account) {
    return accountFeignApi.load(account);
  }

  /**
   * 加载一个对象Account
   *
   * @param id id*
   * @param code 编码*
   * @param account 账户
   * @return Account
   */
  @Override
  public Account load(java.lang.Long id, java.lang.String code, java.lang.String account) {
    if (id == null && code == null && account == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }

    Map<String, Object> param = new HashMap<>();
    param.put("id", id);
    param.put("code", code);
    param.put("account", account);
    return accountObjDAO.load(param);
  }

  /**
   * 加载一个对象Account 通过id
   *
   * @param id id
   * @return Account
   */
  @Override
  public Account loadById(java.lang.Long id) {
    if (id == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    return accountObjDAO.loadById(id);
  }

  /**
   * 加载一个对象Account 通过code
   *
   * @param code 编码
   * @return Account
   */
  @Override
  public Account loadByCode(java.lang.String code) {
    if (code == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    return accountObjDAO.loadByCode(code);
  }

  /**
   * 加载一个对象Account 通过account
   *
   * @param account 账户
   * @return Account
   */
  @Override
  public Account loadByAccount(java.lang.String account) {
    if (account == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    return accountObjDAO.loadByAccount(account);
  }

  /**
   * 根据主键id,oldStates 共同更新 Account 的状态到newState状态
   *
   * @param id id
   * @param newState 新状态
   * @param oldStates 旧状态集合
   */
  @Override
  public void updateStateById(java.lang.Long id, AccountState newState, AccountState... oldStates) {
    if (id == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    if (newState == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    if (oldStates == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    String[] states = new String[oldStates.length];
    for (int i = 0; i < oldStates.length; i++) {
      states[i] = oldStates[i].name();
    }
    accountObjDAO.updateStateById(id, newState.name(), states);
  }

  /**
   * 根据主键code,oldStates 共同更新 Account 的状态到newState状态
   *
   * @param code 编码
   * @param newState 新状态
   * @param oldStates 旧状态集合
   */
  @Override
  public void updateStateByCode(
      java.lang.String code, AccountState newState, AccountState... oldStates) {
    if (code == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    if (newState == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    if (oldStates == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    String[] states = new String[oldStates.length];
    for (int i = 0; i < oldStates.length; i++) {
      states[i] = oldStates[i].name();
    }
    accountObjDAO.updateStateByCode(code, newState.name(), states);
  }

  /**
   * 根据主键account,oldStates 共同更新 Account 的状态到newState状态
   *
   * @param account 账户
   * @param newState 新状态
   * @param oldStates 旧状态集合
   */
  @Override
  public void updateStateByAccount(
      java.lang.String account, AccountState newState, AccountState... oldStates) {
    if (account == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    if (newState == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    if (oldStates == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    String[] states = new String[oldStates.length];
    for (int i = 0; i < oldStates.length; i++) {
      states[i] = oldStates[i].name();
    }
    accountObjDAO.updateStateByAccount(account, newState.name(), states);
  }

  /**
   * 根据主键id 更新 Account 的状态到另一个状态
   *
   * @param id id
   * @param state 状态
   */
  @Override
  public void updateById(java.lang.Long id, AccountState state) {
    if (id == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    accountObjDAO.updateById(id, state.name());
  }

  /**
   * 根据主键code 更新 Account 的状态到另一个状态
   *
   * @param code 编码
   * @param state 状态
   */
  @Override
  public void updateByCode(java.lang.String code, AccountState state) {
    if (code == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    accountObjDAO.updateByCode(code, state.name());
  }

  /**
   * 根据主键account 更新 Account 的状态到另一个状态
   *
   * @param account 账户
   * @param state 状态
   */
  @Override
  public void updateByAccount(java.lang.String account, AccountState state) {
    if (account == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    accountObjDAO.updateByAccount(account, state.name());
  }

  /**
   * 删除对象Account
   *
   * @param id id*
   * @param code 编码*
   * @param account 账户
   */
  @Override
  public void delete(java.lang.Long id, java.lang.String code, java.lang.String account) {
    if (id == null && code == null && account == null) {
      throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
    }
    Map<String, Object> param = new HashMap<>();
    param.put("id", id);
    param.put("code", code);
    param.put("account", account);
    accountObjDAO.delete(param);
  }

  /**
   * 查询Account分页
   *
   * @param accountObj 对象
   * @param offset 查询开始行
   * @param limit 查询行数
   * @return List<Account>
   */
  @Override
  public List<Account> list(Account accountObj, int offset, int limit) {
    if (offset < 0) {
      offset = 0;
    }

    if (limit < 0) {
      limit = Page.limit;
    }

    Map<String, Object> map;
    if (accountObj != null) {
      map =
          JSON.parseObject(JSON.toJSONString(accountObj, SerializerFeature.WriteDateUseDateFormat));
    } else {
      map = new HashedMap();
    }
    return accountObjDAO.list(map, new RowBounds(offset, limit));
  }

  /**
   * 统计
   *
   * @param accountObj 账户信息
   * @return int
   */
  @Override
  public int count(Account accountObj) {
    Map<String, Object> map;
    if (accountObj != null) {
      map =
          JSON.parseObject(JSON.toJSONString(accountObj, SerializerFeature.WriteDateUseDateFormat));
    } else {
      map = new HashedMap();
    }
    return accountObjDAO.count(map);
  }

  /**
   * 查询Account分页
   *
   * @param id id* @param code 编码* @param account 账户
   * @param offset 查询开始行
   * @param limit 查询行数
   * @return List<Account>
   */
  @Override
  public List<Account> list(
      java.lang.Long id, java.lang.String code, java.lang.String account, int offset, int limit) {
    if (offset < 0) {
      offset = 0;
    }

    if (limit < 0) {
      limit = Page.limit;
    }
    Map<String, Object> param = new HashMap<>();
    param.put("id", id);
    param.put("code", code);
    param.put("account", account);
    return accountObjDAO.list(param, new RowBounds(offset, limit));
  }

  @Override
  public int count(java.lang.Long id, java.lang.String code, java.lang.String account) {
    Map<String, Object> param = new HashMap<>();
    param.put("id", id);
    param.put("code", code);
    param.put("account", account);
    return accountObjDAO.count(param);
  }

  /**
   * 根据账号和密码查询
   *
   * @param account 账号
   * @param password 密码
   * @return Account
   */
  @Override
  public Account loadByAccountAndPwd(String account, String password) {
    Map<String, String> paraMap = new HashMap<>();
    paraMap.put("account", account);
    return accountObjDAO.loadByAccountAndPwd(paraMap);
  }
}
